package com.ruoyi.web.controller.lucky;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.SnDrawConfig;
import com.ruoyi.system.service.ISnDrawConfigService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 抽奖配置Controller
 *
 * @author ruoyi
 * @date 2022-12-27
 */
@Controller
@RequestMapping("/system/draw")
public class SnDrawConfigController extends BaseController {
    private String prefix = "system/draw";

    @Autowired
    private ISnDrawConfigService snDrawConfigService;

    @RequiresPermissions("system:draw:view")
    @GetMapping()
    public String draw() {
        return prefix + "/config";
    }

    /**
     * 查询抽奖配置列表
     */
    @RequiresPermissions("system:draw:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SnDrawConfig snDrawConfig) {
        startPage();
        List<SnDrawConfig> list = snDrawConfigService.selectSnDrawConfigList(snDrawConfig);
        return getDataTable(list);
    }

    /**
     * 导出抽奖配置列表
     */
    @RequiresPermissions("system:draw:export")
    @Log(title = "抽奖配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SnDrawConfig snDrawConfig) {
        List<SnDrawConfig> list = snDrawConfigService.selectSnDrawConfigList(snDrawConfig);
        ExcelUtil<SnDrawConfig> util = new ExcelUtil<SnDrawConfig>(SnDrawConfig.class);
        return util.exportExcel(list, "抽奖配置数据");
    }

    /**
     * 新增抽奖配置
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存抽奖配置
     */
    @RequiresPermissions("system:draw:add")
    @Log(title = "抽奖配置", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SnDrawConfig snDrawConfig) {
        return toAjax(snDrawConfigService.insertSnDrawConfig(snDrawConfig));
    }

    /**
     * 修改抽奖配置
     */
    @RequiresPermissions("system:draw:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        SnDrawConfig snDrawConfig = snDrawConfigService.selectSnDrawConfigById(id);
        mmap.put("snDrawConfig", snDrawConfig);
        return prefix + "/edit";
    }

    /**
     * 修改保存抽奖配置
     */
    @RequiresPermissions("system:draw:edit")
    @Log(title = "抽奖配置", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SnDrawConfig snDrawConfig) {
        return toAjax(snDrawConfigService.updateSnDrawConfig(snDrawConfig));
    }

    /**
     * 查看详细
     */
    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") Long id, ModelMap mmap) {
        mmap.put("drawConfig", snDrawConfigService.selectSnDrawConfigById(id));
        return prefix + "/detail";
    }

    /**
     * 删除抽奖配置
     */
    @RequiresPermissions("system:draw:remove")
    @Log(title = "抽奖配置", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(snDrawConfigService.deleteSnDrawConfigByIds(ids));
    }
}
